package com.dfbz.springboot_project.controller;

import com.dfbz.springboot_project.entity.Privilege;
import com.dfbz.springboot_project.entity.Result;
import com.dfbz.springboot_project.entity.Role;
import com.dfbz.springboot_project.service.RolePrivilegeService;
import com.dfbz.springboot_project.service.RoleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @Autowired
    RolePrivilegeService rolePrivilegeService;
    @RequestMapping("index")
    public ModelAndView index(){
        return new ModelAndView("/html/role/role_list.html");
    }
    @RequestMapping("selectPage/{pageNum}/{pageSize}")
    public Result selectPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        return new Result(true,"查询成功",roleService.selectPage(pageNum,pageSize));
    }
    @RequestMapping("selectAll")
    public Result selectAll(){
        return new Result(true,"查询成功",roleService.selectAll());
    }
    @RequestMapping(value = "selectRoleNamesByAid",method = RequestMethod.GET)
    public Result selectRoleNamesByAid(@Param("Aid") Integer Aid){
        return new Result(true,"查询成功",roleService.selectRoleNamesByAid(Aid));
    }
    @RequestMapping("toUpdate")
    public ModelAndView toUpdate(){
        return new ModelAndView("/html/role/role_modi.html");
    }
    @RequestMapping(value = "selectOne",method = RequestMethod.GET)
    public Result selectOne(@Param("Rid") Integer Rid){
        return new Result(true,"查询成功",roleService.selectOne(Rid));
    }
    @RequestMapping(value = "doUpdate",method = RequestMethod.PUT)
    public Result doUpdate(@RequestBody Map<String,Object> params){
        roleService.update(params);
        rolePrivilegeService.updateSelective(params);
        return new Result(true,"更新成功",null);
    }
    @RequestMapping("doDelete/{Rid}")
    public Result doDelete(@PathVariable Integer Rid){
        rolePrivilegeService.deleteByRid(Rid);
        roleService.deleteById(Rid);
        return new Result(true,"删除成功",null);
    }
    @RequestMapping("toInsert")
    public ModelAndView toInsert(){
        return new ModelAndView("/html/role/role_add.html");
    }
    @RequestMapping("doInsert")
    public Result doInsert(@RequestBody Map<String,Object> params){
        System.out.println(params.get("privileges").getClass().getName());
        roleService.InsertSelective(params);
        return new Result(true,"添加成功",null);
    }
}
